#include <benchmark/benchmark.h>
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "unistd.h"
#include "util.h"

using namespace std;

static void Bm_function_Fcntl(benchmark::State &state)
{
    int fd = open("/etc/passwd", O_RDONLY);
    if (fd == -1)
    {
        perror("open fcntl");
        exit(EXIT_FAILURE);
    }
    for (auto _ : state)
    {
        benchmark::DoNotOptimize(fcntl(fd, F_GETFL));
    }
    close(fd);
    state.SetItemsProcessed(state.iterations());
}

static void Bm_function_Open64(benchmark::State &state)
{
    const char *filename = "/proc/self/cmdline";
    for (auto _ : state)
    {
        int fd = open(filename, O_RDONLY);
        if (fd == -1)
        {
            perror("open proc");
            exit(EXIT_FAILURE);
        }
        benchmark::DoNotOptimize(fd);
        close(fd);
    }
    state.SetItemsProcessed(state.iterations());
}

MUSL_BENCHMARK(Bm_function_Fcntl);
MUSL_BENCHMARK(Bm_function_Open64);